"""
客户模型
"""
from datetime import datetime
from app.extensions import db


class Customer(db.Model):
    """客户表"""
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, comment='客户姓名')
    project_number = db.Column(db.String(50), comment='项目序号')
    gender = db.Column(db.String(10), comment='性别')
    start_date = db.Column(db.Date, comment='合作开始时间')
    end_date = db.Column(db.Date, comment='合作结束时间')
    cooperation_days = db.Column(db.Integer, comment='合作天数')
    suitable_manager = db.Column(db.String(100), comment='适宜项目负责人')
    notes = db.Column(db.Text, comment='备注信息')
    is_cooperation_ended = db.Column(db.Boolean, default=False, comment='是否结束合作')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 为兼容性添加的字段（可能在模板中使用）
    industry = db.Column(db.String(100), comment='所属行业')
    contact_person = db.Column(db.String(100), comment='联系人')
    contact_phone = db.Column(db.String(20), comment='联系电话')
    contact_email = db.Column(db.String(100), comment='联系邮箱')
    address = db.Column(db.Text, comment='地址')
    description = db.Column(db.Text, comment='描述')
    contact_info = db.Column(db.String(200), comment='联系信息')
    
    def calculate_cooperation_days(self):
        """自动计算合作天数"""
        if self.start_date:
            end_date = self.end_date or datetime.utcnow().date()
            delta = end_date - self.start_date
            self.cooperation_days = delta.days
            return self.cooperation_days
        return 0
    
    def __repr__(self):
        return f'<Customer {self.name}>'
